<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
    mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
    components: {
        SelectImageRadio: async () =>
            Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectImageRadio()),
        SelectDataGoodsGroup: async () =>
            Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoodsGroup()),
        SelectDataGoodsClassify: async () =>
            Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoodsClassify()),
    },
    data() {
        return {
            appWindow: window,
            form: {
                productGroupsLst: null,
                productCategoryLst: null,
                freight_template_id: null,
            },
            options: [],

            // 
            editVisible: false,

            // 选择要导入的商品id
            selectId: [],

            // 分组所有数据
            groupsList: [],
            // 选中的分组id（经过处理）
            groups: [],
        };
    },
    methods: {
        async getGroups() {
            this.groupsList = await this.$vFN.Tools.getSelectListSource({
                domain: "APP_MAIN_DOMAIN",
                url: "mall-site-portal/product/listGroups",
            },this.appWindow);
        },
        // 验证 
        validate() {
            return new Promise((resolve, reject) => {
                this.$refs["editForm"].validate((valid) => {
                    valid ? resolve(true) : resolve(false)
                });
            });
        },
        // 
        async addProduct(data) {
            this.form.productGroupsLst = [];
            this.groups.forEach(item => {
                if(item.length == 2) {
                    this.form.productGroupsLst.push({
                        parentGroupsId: item[0],
                        groupsId: item[1],
                    })
                }
            });
            console.log(this.form.productGroupsLst);
            if (!await this.validate()) {
                return
            }
            let res = await this.$vFN.Http.post({
                url: "plugin/supply/SupplyGoods/joinGoods",
                data: {
                    ids: this.selectId,
                    category_id: this.form.productCategoryLst[0],
                    sub_category_id: this.form.productCategoryLst[1],
                    freight_template_id: this.form.freight_template_id,
                    groups_ids: this.form.productGroupsLst,
                }
            },window);
            if(res.code == 200) {
                this.$vFN.Tools.messageSuccess("操作成功");
                this.onSuccess();
                this.cacel();
            }
        },

        async getFreight() {
            let res = await this.$vFN.Http.post({
                domain: "APP_MAIN_DOMAIN",
                url: "mall-site-portal/freight-template/list?pageNum=1&pageSize=30",
            },window);
            this.options = res.data.list;
        },

        // 初始化
        async init() {
            this.getGroups();
            this.getFreight();
            this.checkList.forEach(item => {
                this.selectId.push(item.id);
            });
            // console.log(this.selectId);
            // 显示窗口
            this.editVisible = true;
        },
        onSuccess() {

        },
        cacel() {
            this.editVisible = false;
        }

    },
    mounted() {
        this.init();
    },
    activated() {},
};
</script>